/**
 * class VideoSearchEngineView
 * @param entityListView
 * @param controller
 */
function VideoSearchEngineView(controller) {

	SearchEngineView.call(this, controller);
	
	this.addHead = function() {
		var controlsTable = document.createElement("table");
//		controlsTable.border = 1;
		controlsTable.className = "controls";
		var controlsTbody = document.createElement("tbody");

		var controlsTr = document.createElement("tr");

		var searchTitleTd = document.createElement("td");
		searchTitleTd.className = "search-title";
		var searchTitleNode = document.createTextNode("Buscar por título y descripción: ");
		searchTitleTd.appendChild(searchTitleNode);

		controlsTr.appendChild(searchTitleTd);

		var searchTextTd = document.createElement("td");
		searchTextTd.className = "search-text";

		this.textInput = document.createElement("input");
		this.textInput.className = "search-text";
		this.textInput.type = "text";
		this.textInput.onkeypress = (function(obj, method) {
			return function(event) {
				var keyCode = (event.keyCode) ? event.keyCode : event.which;
				eval("obj." + method + "(keyCode);");
			};
		} )(this, "onKeyPress");
		searchTextTd.appendChild(this.textInput);

		controlsTr.appendChild(searchTextTd);

		var searchButtonTd = document.createElement("td");

		var buttonInput = document.createElement("input");
		buttonInput.className = "search-button";
		buttonInput.type = "button";
		buttonInput.value = "Buscar";
		buttonInput.onclick = (function(obj, method) {
			return function() {
				eval("obj." + method + ";");
			};
		})(this, "makeSearch()");

		searchButtonTd.appendChild(buttonInput);

		controlsTr.appendChild(searchButtonTd);

		controlsTbody.appendChild(controlsTr);
		
		controlsTable.appendChild(controlsTbody);

		var tr = document.createElement("tr");
		var th = document.createElement("th");
		th.className = "controls";

		th.appendChild(controlsTable);
		tr.appendChild(th);
		this.thead.appendChild(tr);
	};

	this.onKeyPress = function(keyCode) {
		if (13 == keyCode) {
			this.makeSearch();
		}
	};
	
	this.makeSearch = function() {
		var keywords = this.textInput.value;
		var params = {"keywords" : keywords};
		this.controller.callMethod([this, "setSearchResults"], "search", params);
	};

	this.setSearchResults = function(results) {
		var entityViews = [];
		for (var i=0 ; i<results.length ; i++) {
			var entityView = new VideoDescriptionView(results[i]);
			entityViews.push(entityView);
		}

		// Ordenar aleatoriamente los elementos del array
		entityViews.sort( function() {
			return (Math.round(Math.random())-0.5);
		});
		
		this.entityListView.setEntityViews(entityViews);
	};

	this.createEntityList = function() {
//		this.controller.callMethod([this, "createEntityListDone"], "getEntityListId", {}, Controller.JSON);
//	};
//	
//	this.createEntityListDone = function(controller) {
		var entityListController = new Controller("api.php", "EntityListController");//controller.uri, controller.name, controller.id);
		var pagination = new Pagination(/* elemetsByPage= */ 5, /* interfacesQty= */ 2, /* itemsName= */ "Videos", /* editable= */ false);
		var entityListView = new EntityListView(entityListController, [], pagination);
		
		this.setEntityListView(entityListView);
	};
	
	// /////////////////////////////////////////////////
	// data part (attributes)
	this.textInput = null;
	// /////////////////////////////////////////////////

	// /////////////////////////////////////////////////
	// Constructor body
	this.addHead();
	this.createEntityList();
	this.makeSearch();
	// /////////////////////////////////////////////////
}
